public class Condition {

	private Condition cond1;
	private Condition cond2;
	private Compare com;
	private boolean isNot;
	private String condition;

	public Condition(Tokens tokens) {
		this.cond1 = null;
		this.cond2 = null;
		this.com = null;
		this.isNot = false;
		this.parse(tokens);
	}

	public void parse(Tokens tokens) {
		// Check for '!'
		if (tokens.getToken() == 15) {
			tokens.skipToken();
			this.cond1 = new Condition(tokens);
			this.isNot = true;
		} else if (tokens.getToken() == 16) { // Check for '['
			tokens.skipToken();
			this.cond1 = new Condition(tokens);
			if (tokens.getToken() != 18 && tokens.getToken() != 19) {
				System.err
						.println("There must be a condition '&&' or '||' before token: "
								+ tokens.idName());
				System.exit(0);
			}
			this.condition = tokens.idName();
			tokens.skipToken();
			this.cond2 = new Condition(tokens);
			// Check for ']'
			if (tokens.getToken() != 17) {
				System.err.println("Condition must end with ']' before token: "
						+ tokens.idName());
				System.exit(0);
			}
			tokens.skipToken();
		} else {
			com = new Compare(tokens);
		}
	}

	public boolean runCond() {
		boolean flag;
		// if condition is 'not'
		if (this.isNot) {
			flag = !(this.cond1.runCond());
		} else if (this.cond1 != null && this.cond2 != null) {
			boolean tmp1 = this.cond1.runCond();
			boolean tmp2 = this.cond2.runCond();
			// condition is 'and'
			if (this.condition.compareTo("&&") == 0) {
				flag = (tmp1 && tmp2);
			} else { // condition is 'or'
				flag = (tmp1 || tmp2);
			}
		} else {
			flag = com.runComp();
		}
		return flag;
	}
}